package mia.beans;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.hibernate.Query;
import org.hibernate.Session;

import mia.dao.AdvertisementTypeEntityDAO;
import mia.entity.AdvertisementTypeEntity;
import mia.util.HibernateManager;

@ManagedBean(name="advertisementTypeBean")
@RequestScoped
public class AdvertisementTypeBean {

	private Map<String, AdvertisementTypeEntity> types = null;
	private List<AdvertisementTypeEntity> typesList = null;
	private AdvertisementTypeEntity selectedType = null;
	
	public Map<String, AdvertisementTypeEntity> getTypes() {
		types = AdvertisementTypeEntityDAO.getAdvertisementTypes();
		return types;
	}
	
	public void deleteType()
	{
		Session session = HibernateManager.getSession();
		session.beginTransaction();
		Query q = session.createQuery("delete from AdvertisementEntity where type = :typ");
		q.setInteger("typ", selectedType.getType_id());
		q.executeUpdate();
		Query q2 = session.createQuery("delete from AdvertisementTypeEntity where type_id = :typ");
		q2.setInteger("typ", selectedType.getType_id());
		q2.executeUpdate();
		session.getTransaction().commit();
	}

	public AdvertisementTypeEntity getSelectedType() {
		return selectedType;
	}
	public void setSelectedType(AdvertisementTypeEntity selectedType) {
		this.selectedType = selectedType;
	}

	public void setTypes(Map<String, AdvertisementTypeEntity> types) {
		this.types = types;
	}
	public List<AdvertisementTypeEntity> getTypesList() {
		typesList = AdvertisementTypeEntityDAO.getAdvertisementTypesList();
		return typesList;
	}	
}
